package ranab.server;

import java.net.*;

/**
 * Debug client to send/receive messages
 */
public class DebugClient {

    private int mPort;
    private InetAddress mAddr;

    public DebugClient(int port, InetAddress addr) {
        mPort = port;
        mAddr = addr;
    }
    
    
    /**
     * Start data transfer 
     */
    public void start() throws Exception {
        Socket soc = null;
        try {
            soc = new Socket(mAddr, mPort);
            MessageEchoer echoer = new MessageEchoer(soc);
            echoer.start();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        finally {
            if(soc != null) {
                try {
                    soc.close();
                }
                catch(Exception ex) {
                }
            }
        }
    }
    
    
    /**
     * Starting point
     */
    public static void main(String args[]) {
        
        // input argument check
        if(args.length != 2) {
            System.out.println("Usage: java ranab.server.DebugClient <port> <address>");
            System.exit(1);
        }
        
        try {
            int port = Integer.parseInt(args[0]);
            InetAddress addr = InetAddress.getByName(args[1]);
            
            DebugClient client = new DebugClient(port, addr);
            client.start();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
